package com.example.es.util;

import com.example.es.entity.SmsLogs;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.common.xcontent.json.JsonXContent;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @description:  term query
 * @author: ruoan
 * @date: 2020/10/11 11:57
 */
public class EsRestHighLevelClientTest6 {

    private RestHighLevelClient client= EsRestHighLevelClient.getClient();
    ObjectMapper mapper = new ObjectMapper();
    String index = "sms-logs-index";
    String type="sms-logs-type";

    /**
     * term query
     */
    @Test
    public void term() throws IOException {
        //创建SearchRequest
        SearchRequest searchRequest = new SearchRequest(index);
        searchRequest.types(type);
        //创建SearchSourceBuilder
        SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
        searchSourceBuilder.from(0);
        searchSourceBuilder.size(4);
        //将QueryBuilders构建的查询条件放入searchSourceBuilder中
        searchSourceBuilder.query(QueryBuilders.termQuery("province","北京"));
        //将SearchSourceBuilder放入到SearchRequest中
        searchRequest.source(searchSourceBuilder);

        //客户端执行search方法，放入查询请求
        SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
        //SearchResponse获取到结果，通过迭代获取到内容
        for (SearchHit hit : searchResponse.getHits().getHits()) {
            Map<String, Object> sourceAsMap = hit.getSourceAsMap();
            System.out.println(sourceAsMap);
        }



    }







}